<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>dataset.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_dataset.js.html">dataset.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities for adding, removing and setting values in
<a name="line17"></a> * an Element&#39;s dataset.
<a name="line18"></a> * See {@link http://www.w3.org/TR/html5/Overview.html#dom-dataset}.
<a name="line19"></a> *
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.dom.dataset&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.string&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>/**
<a name="line28"></a> * The DOM attribute name prefix that must be present for it to be considered
<a name="line29"></a> * for a dataset.
<a name="line30"></a> * @type {string}
<a name="line31"></a> * @const
<a name="line32"></a> * @private
<a name="line33"></a> */
<a name="line34"></a>goog.dom.dataset.PREFIX_ = &#39;data-&#39;;
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * Sets a custom data attribute on an element. The key should be
<a name="line39"></a> * in camelCase format (e.g &quot;keyName&quot; for the &quot;data-key-name&quot; attribute).
<a name="line40"></a> * @param {Element} element DOM node to set the custom data attribute on.
<a name="line41"></a> * @param {string} key Key for the custom data attribute.
<a name="line42"></a> * @param {string} value Value for the custom data attribute.
<a name="line43"></a> */
<a name="line44"></a>goog.dom.dataset.set = function(element, key, value) {
<a name="line45"></a>  if (element.dataset) {
<a name="line46"></a>    element.dataset[key] = value;
<a name="line47"></a>  } else {
<a name="line48"></a>    element.setAttribute(
<a name="line49"></a>        goog.dom.dataset.PREFIX_ + goog.string.toSelectorCase(key),
<a name="line50"></a>        value);
<a name="line51"></a>  }
<a name="line52"></a>};
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Gets a custom data attribute from an element. The key should be
<a name="line57"></a> * in camelCase format (e.g &quot;keyName&quot; for the &quot;data-key-name&quot; attribute).
<a name="line58"></a> * @param {Element} element DOM node to get the custom data attribute from.
<a name="line59"></a> * @param {string} key Key for the custom data attribute.
<a name="line60"></a> * @return {?string} The attribute value, if it exists.
<a name="line61"></a> */
<a name="line62"></a>goog.dom.dataset.get = function(element, key) {
<a name="line63"></a>  if (element.dataset) {
<a name="line64"></a>    return element.dataset[key];
<a name="line65"></a>  } else {
<a name="line66"></a>    return element.getAttribute(goog.dom.dataset.PREFIX_ +
<a name="line67"></a>                                goog.string.toSelectorCase(key));
<a name="line68"></a>  }
<a name="line69"></a>};
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Removes a custom data attribute from an element. The key should be
<a name="line74"></a>  * in camelCase format (e.g &quot;keyName&quot; for the &quot;data-key-name&quot; attribute).
<a name="line75"></a> * @param {Element} element DOM node to get the custom data attribute from.
<a name="line76"></a> * @param {string} key Key for the custom data attribute.
<a name="line77"></a> */
<a name="line78"></a>goog.dom.dataset.remove = function(element, key) {
<a name="line79"></a>  if (element.dataset) {
<a name="line80"></a>    delete element.dataset[key];
<a name="line81"></a>  } else {
<a name="line82"></a>    element.removeAttribute(goog.dom.dataset.PREFIX_ +
<a name="line83"></a>                            goog.string.toSelectorCase(key));
<a name="line84"></a>  }
<a name="line85"></a>};
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * Checks whether custom data attribute exists on an element. The key should be
<a name="line90"></a> * in camelCase format (e.g &quot;keyName&quot; for the &quot;data-key-name&quot; attribute).
<a name="line91"></a> *
<a name="line92"></a> * @param {Element} element DOM node to get the custom data attribute from.
<a name="line93"></a> * @param {string} key Key for the custom data attribute.
<a name="line94"></a> * @return {boolean} Whether the attibute exists.
<a name="line95"></a> */
<a name="line96"></a>goog.dom.dataset.has = function(element, key) {
<a name="line97"></a>  if (element.dataset) {
<a name="line98"></a>    return key in element.dataset;
<a name="line99"></a>  } else if (element.hasAttribute) {
<a name="line100"></a>    return element.hasAttribute(goog.dom.dataset.PREFIX_ +
<a name="line101"></a>                                goog.string.toSelectorCase(key));
<a name="line102"></a>  } else {
<a name="line103"></a>    return !!(element.getAttribute(goog.dom.dataset.PREFIX_ +
<a name="line104"></a>                                   goog.string.toSelectorCase(key)));
<a name="line105"></a>  }
<a name="line106"></a>};
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * Gets all custom data attributes as a string map.  The attribute names will be
<a name="line111"></a> * camel cased (e.g., data-foo-bar -&gt; dataset[&#39;fooBar&#39;]).  This operation is not
<a name="line112"></a> * safe for attributes having camel-cased names clashing with already existing
<a name="line113"></a> * properties (e.g., data-to-string -&gt; dataset[&#39;toString&#39;]).
<a name="line114"></a> * @param {!Element} element DOM node to get the data attributes from.
<a name="line115"></a> * @return {!Object} The string map containing data attributes and their
<a name="line116"></a> *     respective values.
<a name="line117"></a> */
<a name="line118"></a>goog.dom.dataset.getAll = function(element) {
<a name="line119"></a>  if (element.dataset) {
<a name="line120"></a>    return element.dataset;
<a name="line121"></a>  } else {
<a name="line122"></a>    var dataset = {};
<a name="line123"></a>    var attributes = element.attributes;
<a name="line124"></a>    for (var i = 0; i &lt; attributes.length; ++i) {
<a name="line125"></a>      var attribute = attributes[i];
<a name="line126"></a>      if (goog.string.startsWith(attribute.name,
<a name="line127"></a>                                 goog.dom.dataset.PREFIX_)) {
<a name="line128"></a>        // We use substr(5), since it&#39;s faster than replacing &#39;data-&#39; with &#39;&#39;.
<a name="line129"></a>        var key = goog.string.toCamelCase(attribute.name.substr(5));
<a name="line130"></a>        dataset[key] = attribute.value;
<a name="line131"></a>      }
<a name="line132"></a>    }
<a name="line133"></a>    return dataset;
<a name="line134"></a>  }
<a name="line135"></a>};
</pre>


</body>
</html>
